const path = require('path')

const Koa = require('koa')
const KoaBody = require('koa-body')
const parameter = require('koa-parameter')
const KoaStatic = require('koa-static')

const errHandler = require('./errHandler')

const router = require('../router')

const app = new Koa()

app
  .use(KoaBody({
    multipart: true,
    formidable: {
      uploadDir: path.join(__dirname, '../upload'),
      keepExtensions: true
    }
  }))
  .use(KoaStatic(path.join(__dirname, '../upload')))
  .use(parameter(app))
  .use(router.routes())

  // 统一错误处理
  .on('error', errHandler)

module.exports = app